<template>
  <div>
    <NavBack title="修改密码"></NavBack>

    <van-form @submit="submitForm">
      <van-field type="password" label="原密码" v-model="form.pwd" placeholder="请输入原密码"
                 :rules="[{required:true}]"></van-field>
      <van-field type="password" label="新密码" v-model="form.npwd" placeholder="请输入新密码"
                 :rules="[{required:true}]"></van-field>
      <van-field type="password" label="重复新密码" v-model="form.cnpwd" placeholder="请输入重复新密码"
                 :rules="[{required:true,validator:cpwdValid}]"></van-field>

      <div style="margin: 1rem">
        <van-button round block type="info" :loading="form.submit">保存</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import NavBack from "../common/NavBack";
import {status, user} from "../../request/api";

export default {
  name: "UpdatePwd",
  data() {
    return {form: {}};
  },
  components: {NavBack},
  methods: {
    pageBack() {
      this.$router.back();
    },
    cpwdValid(value) {
      return value == this.form.npwd;
    },
    async submitForm() {
      this.form.submit = true;
      const res = await user.updatePwd(this.form);

      this.form.submit = false;
      if (res.data.code == status.STATUS_SUCCESS) {
        this.$notify({type: 'success', message: res.data.msg});
        setTimeout(() => {
          this.$router.back();
        }, 2000);
      }
    }
  }
}
</script>

<style scoped>

</style>
